package com.asa.floatview

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.constraintlayout.widget.ConstraintLayout
import kotlin.math.abs

/**
 * create By：anderson
 * on 2022/5/2
 * desc:
 */
class InterceptorContainer @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {


    private var downX = 0
    private var downY = 0
    private var touchSlop = 0

    init {
        touchSlop  = ViewConfiguration.get(context).scaledTouchSlop
    }


    override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
        event?.also {
            val action = it.action
            if (action == MotionEvent.ACTION_DOWN){
                downX = event.x.toInt()
                downY = event.y.toInt()
            } else if (action == MotionEvent.ACTION_MOVE){
                val moveX = event.x.toInt()
                val moveY = event.y.toInt()
                val distance = moveY - downY
                if (abs(moveX - downX) < abs(distance)){
                    Log.e("日志", "onInterceptTouchEvent: distance:${distance}  touchSlop:${touchSlop}")
                    if (abs(distance)> touchSlop){
                        return true
                    }
                }
            }
        }
        return super.onInterceptTouchEvent(event)
    }

}